﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Gpc;
using YJKRevitKernel.RevitTopologySuite.GPCAdapter;

namespace YJKPatternSign.StairsRoomSection.Kit
{
	public class GpcPolygonContrllor
	{
		public static List<List<List<Curve>>> MergeAreas(List<List<Curve>> areaCureOrg)
		{
			List<List<List<Curve>>> result = new List<List<List<Curve>>>();
			if (areaCureOrg.Count < 1)
			{
				return result;
			}
			List<IPolygon> list = new List<IPolygon>();
			Transform.Identity.ScaleBasis(1.0);
			for (int i = 0; i < areaCureOrg.Count; i++)
			{
				List<Curve> list2 = new List<Curve>();
				foreach (Curve item in areaCureOrg[i])
				{
					list2.Add(item);
				}
				Polygon item2 = GpcPolygonController.ConstructPolygon(list2, null);
				list.Add(item2);
			}
			List<List<List<Curve>>> list3 = GpcPolygonContrllor.MergeAreas(list);
			List<List<List<Curve>>> list4 = new List<List<List<Curve>>>();
			foreach (List<List<Curve>> list5 in list3)
			{
				List<List<Curve>> list6 = new List<List<Curve>>();
				foreach (List<Curve> list7 in list5)
				{
					List<Curve> list8 = new List<Curve>();
					foreach (Curve item3 in list7)
					{
						list8.Add(item3);
					}
					list6.Add(list8);
				}
				list4.Add(list6);
			}
			return list4;
		}

		private static List<List<List<Curve>>> MergeAreas(List<IPolygon> areaAll)
		{
			List<List<List<Curve>>> list = new List<List<List<Curve>>>();
			if (areaAll.Count == 1)
			{
				return GpcPolygonController.PolygonToArarCurve(areaAll[0]);
			}
			new List<IPolygon>().Add(areaAll[0]);
			LinkedList<IPolygon> linkedList = new LinkedList<IPolygon>();
			LinkedList<IPolygon> linkedList2 = new LinkedList<IPolygon>(areaAll);
			while (linkedList2.Count<IPolygon>() > 0)
			{
				LinkedListNode<IPolygon> node = new LinkedListNode<IPolygon>(linkedList2.First.Value);
				linkedList.AddFirst(node);
				linkedList2.RemoveFirst();
				GpcPolygonContrllor.MergeAreas(linkedList, linkedList2);
			}
			foreach (IPolygon intPolygon in linkedList)
			{
				list.AddRange(GpcPolygonController.PolygonToArarCurve(intPolygon));
			}
			return list;
		}

		private static void MergeAreas(LinkedList<IPolygon> PolygonResult, LinkedList<IPolygon> PolygonTmp)
		{
			LinkedListNode<IPolygon> linkedListNode = PolygonResult.First;
			LinkedListNode<IPolygon> last = PolygonResult.Last;
			LinkedListNode<IPolygon> linkedListNode2 = PolygonTmp.First;
			while (PolygonTmp.Count > 0)
			{
				linkedListNode2 = PolygonTmp.First;
				int num = 0;
				while (linkedListNode2 != null)
				{
					IPolygon value = linkedListNode.Value;
					IPolygon value2 = linkedListNode2.Value;
					value.ClipPolygon(value2, ClipOp.Int).ToGraphicsPath(ContourType.All, GraphicsPathType.Lines);
					num++;
					IPolygon value3 = value.ClipPolygon(value2, ClipOp.Union);
					PolygonResult.AddLast(value3);
					LinkedListNode<IPolygon> next = linkedListNode.Next;
					PolygonResult.Remove(linkedListNode);
					linkedListNode = next;
					LinkedListNode<IPolygon> next2 = linkedListNode2.Next;
					PolygonTmp.Remove(linkedListNode2);
					linkedListNode2 = next2;
				}
				if (num == 0)
				{
					break;
				}
			}
		}
	}
}
